package com.forezp;

import com.forezp.service.TestService;
import com.forezp.vo.CommonParam;
import com.forezp.vo.TestParam;
import com.mzt.logapi.starter.annotation.EnableLogRecord;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@SpringBootApplication
@RestController
@PropertySource("classpath:bootstrap.yml")
@EnableLogRecord(tenant = "com.mzt.test")
public class ServiceHiApplication {
	@Autowired
	TestService testService;


	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}

	@Value("8762")
	String port;
	@PostMapping("/hi")
	@LogRecord(success = "更新了信息{_DIFF{#order.oldOrder, #order.newOrder}}",
			type = "销售中台", bizNo = "{{#order.newOrder}}",
			extra = "{{#order.newOrder.toString()}}")
	public String home(@RequestBody TestParam order) {

		return testService.test2(order);
	}

	@PostMapping("/hi1")
	@LogRecord(success = "更新了信息{_DIFF{#order.oldOrder.productName, #order.newOrder.productName}}",
			type = "销售中台", bizNo = "{{#order.newOrder}}",
			extra = "{{#order.newOrder.toString()}}")
	public String home2(@RequestBody TestParam order) {

		return testService.test2(order);
	}

	@LogRecord(success = "更新了订单#{ORDER_TEST{#order.newOrder.productName}},#{ORDER_TEST{#order.oldOrder.productName}}更新内容为...",
			type = "测试", bizNo = "{{#order}}",
			extra = "{{#order.toString()}}")
	@PostMapping("/hi2")
	public boolean updateBefore(@RequestBody TestParam order) {
		return false;
	}

	@LogRecord(success = "理由{{#order.getString('creditReviewOpinions')}},更新内容为...",
			type = "测试", bizNo = "{{#order}}",
			extra = "{{#order.toString()}}")
	@PostMapping("/hi3")
	public boolean hi3(@RequestBody CommonParam order) {
		return false;
	}


	@PostMapping("/hi4")
	@LogRecord(success = "更新了信息 {_DIFF{#{ORDER_TEST{'肯德基'}},#{ORDER_TEST{'必胜客'}}}}",
			type = "销售中台", bizNo = "{{#order.newOrder}}",
			extra = "{{#order.newOrder.toString()}}")
	public String hi4(@RequestBody TestParam order) {

		return testService.test2(order);
	}

	@PostMapping("/hi5")
	@LogRecord(success = "更新了信息 {ORDER_TEST{#order.newOrder.productName}},{ORDER_TEST{#order.oldOrder.productName}}",
			type = "销售中台", bizNo = "{{#order.newOrder}}",
			extra = "{{#order.newOrder.toString()}}")
	public String hi5(@RequestBody TestParam order) {

		return testService.test2(order);
	}

	@PostMapping("/hi6")
	@LogRecord(success = "更新了信息 {ORDER_TEST{#order.newOrder.productName}},{ORDER_TEST{#order.oldOrder.productName}}",
			type = "销售中台", bizNo = "{{#order.newOrder}}",
			extra = "{{#order.newOrder.toString()}}")
	public String hi6(@RequestBody TestParam order) {

		return testService.test2(order);
	}






	@RequestMapping("/download")
	public HttpServletResponse download(HttpServletResponse response) {
		String filePath = "C:\\Users\\hcf\\Desktop\\pushSF0521.xlsx";

		try (InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
			 OutputStream toClient = new BufferedOutputStream(response.getOutputStream())) {
			File file = new File(filePath);
			String filename = file.getName();
			response.reset();
			response.setHeader("Content-Disposition", "attachment; filename=" + filename);
			response.setContentType("application/octet-stream");

			byte[] buffer = new byte[1024];
			int bytesRead;
			while ((bytesRead = fis.read(buffer)) != -1) {
				toClient.write(buffer, 0, bytesRead);
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		}

		return response;
	}


}
